package com.qc.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qc.sys.common.ActiveUser;
import com.qc.sys.common.DataGridView;
import com.qc.sys.common.ResultObj;
import com.qc.sys.dao.SysNoticeDao;
import com.qc.sys.entity.SysNotice;
import com.qc.sys.entity.SysUser;
import com.qc.sys.service.SysNoticeService;
import com.qc.sys.vo.NoticeVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;

/**
 * (SysNotice)表服务实现类
 *
 * @author AltmanQccc
 * @since 2020-08-27 19:22:08
 */
@Service
public class SysNoticeServiceImpl extends ServiceImpl<SysNoticeDao, SysNotice> implements SysNoticeService {

    @Autowired
    private SysNoticeDao sysNoticeDao;

    @Override
    public DataGridView queryAllNotice(NoticeVo noticeVo) {
        IPage<SysNotice> page = new Page<>(noticeVo.getPage(),noticeVo.getLimit());
        QueryWrapper<SysNotice> qw = new QueryWrapper<>();
        qw.like(StringUtils.isNotBlank(noticeVo.getTitle()),"title",noticeVo.getTitle());
        qw.like(StringUtils.isNotBlank(noticeVo.getOpername()),"opername",noticeVo.getOpername());
        qw.orderByDesc("createtime");
        sysNoticeDao.selectPage(page,qw);
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    @Override
    public ResultObj InsertNotice(SysNotice notice) {
        try {
            Subject subject = SecurityUtils.getSubject();
            ActiveUser activeUser = (ActiveUser) subject.getPrincipal();
            SysUser user = activeUser.getUser();
            notice.setOpername(user.getName());
            notice.setCreatetime(new Date());
            this.sysNoticeDao.insert(notice);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    @Override
    public ResultObj UpdateNotice(SysNotice notice) {
        try {
            this.sysNoticeDao.updateById(notice);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    @Override
    public ResultObj DeleteNotice(Integer id) {
        try {
            this.sysNoticeDao.deleteById(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    @Override
    public ResultObj BatchDeleteNotice(Integer[] ids) {
        try {
            //1.对ids进行判断，是否为空，是否长度大于0
            if (null != ids & ids.length >0){
                //2.对ids进行循环删除
                ArrayList<Integer> idList = new ArrayList<>();
                for (Integer id : ids) {
                    idList.add(id);
                }
                this.sysNoticeDao.deleteBatchIds(idList);
                return ResultObj.DELETE_SUCCESS;
            }else {
                //3.false执行 {code:-1,msg:"传入ids异常"}
                return new ResultObj(-1,"传入ids异常");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    @Override
    public ResultObj ShowNotice(Integer id) {
        try {
            this.sysNoticeDao.selectById(id);
            return ResultObj.SHOW_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.SHOW_ERROR;
        }
    }
}